package com.lovo.sys.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.commom.vo.ResultVO;
import com.lovo.sys.bean.ProductBean;
import com.lovo.sys.bean.SchemeBean;
import com.lovo.sys.service.ISchemeService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 采购订单controller
 */
@RestController
@RequestMapping("scheme")
public class SchemeController {

    @Autowired
    private ISchemeService schemeService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 分页查询所有商品
     *
     * @param pageNum 页码
     * @return 商品对象集合
     */
    @RequestMapping("productAll")
    public IPage<ProductBean> productAll(Integer pageNum) {
        return schemeService.findAllProduct(pageNum);
    }

    /**
     * 根据商品id查询该商品的库存信息
     *
     * @param productId 商品id
     * @return 商品对象
     */
    @RequestMapping("productId")
    public ProductBean productId(long productId) {
        return schemeService.findByProductId(productId);
    }

    /**
     * 分页查询所有采购订单
     *
     * @return
     */
    @RequestMapping("schemeAll")
    public List<SchemeBean> schemeAll() {
        return schemeService.findAllScheme();
    }

    /**
     * 采购商品
     * 添加采购订单表
     * 把该采购订单信息发送到MQ
     *
     * @return
     */
    @RequestMapping("addScheme")
    public ResultVO addScheme(String procurementReason, String applicant, int num,long productId) {
        SchemeBean schemeBean = new SchemeBean();
        ResultVO rs = new ResultVO();
        ProductBean byProductId = schemeService.findByProductId(productId);
        float money = byProductId.getPrice() * num;
        String str = JSON.toJSONString(byProductId);
        if (byProductId != null) {
            //设置采购采购理由
            schemeBean.setProcurementReason(procurementReason);
            //设置采购申请人
            schemeBean.setApplicant(applicant);
            //设置采购订单
            schemeBean.setContent(str);
            //设置采购总价
            schemeBean.setMoney(money);
            //设置采购数量
            schemeBean.setNum(num);
            //设置采购订单编号
            long l = System.currentTimeMillis();
            String id = String.valueOf(l);
            schemeBean.setIdNum(id);
            schemeService.addScheme(schemeBean);
            //把采购订单对象转换成JSON格式
            String schemeJosn = JSON.toJSONString(schemeBean);
            //把采购订单添加到MQ
            rabbitTemplate.convertAndSend("directSchemeExchange", "rabbit.sheme", schemeJosn);
            rs.setJson("发送成功");
            rs.setTag(0);
            return rs;
        }
        rs.setJson("发送失败");
        rs.setTag(1);
        return rs;
    }

    /**
     * 远程接口，修改审核状态
     * @param idNum
     * @param status
     * @return
     */
    @RequestMapping("updateIdNum")
    public boolean updateIdNum(@RequestParam("idNum") String idNum, @RequestParam("status") int status,@RequestParam("refuse") String refuse){
        schemeService.updateIdNum(idNum, status,refuse);
        return true;
    }
}
